<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
	<style>
  .add-photos > div {
      width: 100%;
      height:2.8rem;
      line-height: 2.8rem;
  }
  .add-photos > div .aui-iconfont {
      font-size: 2rem;
      color: #ccc;
  }
	</style>
</head>
<body>
    <section class="aui-content aui-margin-t-15">
          <ul class="aui-list aui-form-list" id="work-list">
          <!--<li class="aui-list-item">
              <div class="aui-list-item-inner">
                  <div class="aui-list-item-label">
                      Text
                  </div>
                  <div class="aui-list-item-input">
                      <input type="text" placeholder="Name">
                  </div>
              </div>
          </li>
          <li class="aui-list-item">
              <div class="aui-list-item-inner">
                  <div class="aui-list-item-label">
                      Select
                  </div>
                  <div class="aui-list-item-input">
                      <select>
                          <option>Option1</option>
                          <option>Option2</option>
                          <option>Option3</option>
                      </select>
                  </div>
              </div>
          </li>
          <li class="aui-list-item">
              <div class="aui-list-item-inner">
                  <div class="aui-list-item-label">
                      文本
                  </div>
                  <div class="aui-list-item-input">
                      <textarea placeholder="Textarea"></textarea>
                  </div>
              </div>
          </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">
                        时间
                    </div>
                    <div class="aui-list-item-input">
                        <input id="id_time" onclick="selectTime()" readonly="readonly" type="text" placeholder="选择时间">
                    </div>
                </div>
            </li>-->
        </ul>
    </section>
    <div  style="display: block" class="aui-content aui-margin-15">
    <div class="aui-btn aui-btn-info aui-btn-block aui-btn-sm " onclick="addWork()">提交</div>
    </div>
</body>
<script type="text/javascript" src="../script/jquery-1.3.2.min.js" ></script>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/aui-actionsheet.js" ></script>
<script type="text/javascript" src="../script/aui-toast.js" ></script>
<script type="text/javascript">
var toast = new auiToast();
    apiready = function(){
        api.parseTapmode();
        var id = api.pageParam.id;
        api.ajax({
            url: ServeUrl+'m=Workflow&a=editForm',
            method: 'post',
            data : {
                values : {
                    username : $api.getStorage("username"),
                    id : id
                }
            }
        },function(ret, err){
            alert(JSON.stringify(ret));
            var html = '';
          //  $api.setStorage('field_name', '');
            for (var i = 0; i < ret.data.data.length; i++) {

                if(ret.data.data[i].text_type == 1){
                    html += '<li class="aui-list-item">'
                    html += '<div class="aui-list-item-inner">'
                    html += '<div class="aui-list-item-label">'+ret.data.data[i].cn_name+'</div>'
                    html += '<div class="aui-list-item-input">'
                    html += '<input id="id_'+ret.data.data[i].en_name+'"  type="text" value="'+ret.data.arrWD[ret.data.data[i].en_name]+'" name="'+ret.data.data[i].en_name+'" placeholder="请填写'+ret.data.data[i].cn_name+'">'
                    html += '</div>'
                    html += '</div>'
                    html += '</li>'
                }
                if(ret.data.data[i].text_type == 2){

                    html += '<li class="aui-list-item">'
                    html += '<div class="aui-list-item-inner">'
                    html += '<div class="aui-list-item-label">'+ret.data.data[i].cn_name+'</div>'
                    html += '<div class="aui-list-item-input">'
                    html += '<select id="id_select_'+ret.data.data[i].en_name+'" name="'+ret.data.data[i].en_name+'">'
                    //html += '<option value="">请选择</option>'
                    for (var j = 0; j < ret.data.data[i].field_values.length; j++) {
                        if(ret.data.data[i].field_values[j][0] == ret.data.arrWD[ret.data.data[i].en_name]){
                          html += '<option value="'+ret.data.data[i].field_values[j][0]+'">'+ret.data.data[i].field_values[j][1]+'</option>'
                        }
                    }
                    html += '</select>'
                    html += '</div>'
                    html += '</div>'
                    html += '</li>'
                }
                if(ret.data.data[i].text_type == 3){
                    html += '<li class="aui-list-item">'
                    html += '<div class="aui-list-item-inner">'
                    html += '<div class="aui-list-item-label">'+ret.data.data[i].cn_name+'</div>'
                    html += '<div class="aui-list-item-input">'
                    html += '<textarea id="id_'+ret.data.data[i].en_name+'"  name="'+ret.data.data[i].en_name+'" placeholder="请填写'+ret.data.data[i].cn_name+'">'+ret.data.arrWD[ret.data.data[i].en_name]+'</textarea>'
                    html += '</div>'
                    html += '</div>'
                    html += '</li>'
                }
                if(ret.data.data[i].text_type == 4){
                    html += '<li class="aui-list-item">'
                    html += '<div class="aui-list-item-inner">'
                    html += '<div class="aui-list-item-label">'+ret.data.data[i].cn_name+'</div>'
                    html += '<div class="aui-list-item-input">'
                    html += '<input  value="'+ret.data.arrWD[ret.data.data[i].en_name]+'" id="id_'+ret.data.data[i].en_name+'" name="'+ret.data.data[i].en_name+'" onclick="selectTime('+"'"+ret.data.data[i].en_name+"'"+')" readonly="readonly" type="text" placeholder="选择时间">'
                    html += '</div>'
                    html += '</div>'
                    html += '</li>'
                }

              //  $api.setStorage('field_name', ret.data.data[i].en_name+','+$api.getStorage('field_name'));


            }
            if(ret.data.arrData.allow_attachment == 'Y'){
                html += '<li class="aui-list-item">'
                html += '<div class="aui-list-item-inner">'
                html += '<div class="aui-list-item-label">附件</div>'
                html += '<div class="aui-list-item-input">'
                html += '<div class="aui-row aui-row-padded" >'
                html += '<div class="aui-col-xs-3 add-photos"  onclick="openActionsheet()">'
                html += '<div class="aui-border aui-text-center" >'
                html += '<i class="aui-iconfont aui-icon-plus"></i>'
                html += '</div>'
                html += '</div>'
                html += '</div>'
                html += '</div>'
                html += '</div>'
                html += '</li>'
            }
            if(ret.data.arrU){
                html += '<li class="aui-list-item">'
                html += '<div class="aui-list-item-inner">'
                html += '<div class="aui-list-item-label">转交人</div>'
                html += '<div class="aui-list-item-input">'
                html += '<select name="username">'
                html += '<option value="">请选择</option>'
                for (var i = 0; i < ret.data.arrU.length; i++) {
                html += '<option value="'+ret.data.arrU[i].username+'">'+ret.data.arrU[i].cn_name+'</option>'
                }
                html += '</select>'
                html += '</div>'
                html += '</div>'
                html += '</li>'
            }
            html += '<input type="hidden" name="process_design_id" value="'+ret.data.arrData.id+'" />'
            html += '<input type="hidden" name="form_design_id" value="'+ret.data.arrData.form_design_id+'" />'
            html += '<input type="hidden" name="process_node_id" value="'+ret.data.arrData.first_node_id+'" />'
            html += '<input type="hidden" name="allow_attachment" value="'+ret.data.arrData.allow_attachment+'" />'
            html += '<input type="hidden" name="first_node_order_num" value="'+ret.data.arrData.first_node_order_num+'" />'
            html += '<input type="hidden" name="working_name" value="'+ret.data.arrData.working_name+'" />'
            $("#work-list").append(html);


            for (var k = 0; k < ret.data.fieldHidden.length; k++) {
                $("#id_"+ret.data.fieldHidden[k].en_name).attr("readonly",true);
                $("#id_"+ret.data.fieldHidden[k].en_name).css("color","#7A7A7A");
            }
        });

    };

    var actionsheet = new auiActionsheet();
    //打开底部弹窗
    function openActionsheet(){
        actionsheet.init({
            frameBounces:true,//当前页面是否弹动，（主要针对安卓端）
            title:"选择图片",
            cancelTitle:'取消',
            //destructiveTitle:'红色警告按钮',
            buttons:['拍照','图库选择']
        },function(ret){
            if(ret){
                if(ret.buttonIndex=="1"){
                    getPic("camera");
                }else {
                    getPic("library");
                }
            }
        })
    }
    //选择时间
    function selectTime(name){
        api.openPicker({
          type: 'date',
          title: '选择时间'
        }, function(ret, err) {
          if (ret) {
              $("#id_"+name).val(ret.year+'-'+ret.month+'-'+ret.day+' 00:00:00');
              alert(JSON.stringify(ret));
          } else {
              alert(JSON.stringify(err));
          }
        });

    }

    function addWork(){
        var input = document.getElementsByTagName("input");
        var  textarea= document.getElementsByTagName("textarea");
        var  select= document.getElementsByTagName("select");
        //var x = document.getElementsByTagName("input");
        var jsonstr ='{';
        for(var i=0 ;i<input.length;i++){
          jsonstr+='"'+input[i].name+'":"'+input[i].value+'",'
        };
        for(var i=0 ;i<textarea.length;i++){
          jsonstr+='"'+textarea[i].name+'":"'+textarea[i].value+'",'
        };
        for(var i=0 ;i<select.length;i++){
          jsonstr+='"'+select[i].name+'":"'+select[i].value+'",'
        };
        jsonstr = jsonstr.substring(0, jsonstr.length - 1);
        jsonstr +='}';
        var username = $api.getStorage('username');

        alert(jsonstr);
        //var person = '{"LoginName":"admin","Password":"123456"}';
        api.ajax({
            url: ServeUrl+'m=Workflow&a=insertFileForm',
            method: 'post',
            //contentType : 'application/json',
            //dataType: 'json',
            data : {
              values : {
                jsonstr:jsonstr,
                username:username
              }
            },
        },function(ret, err){
            //alert(JSON.stringify(ret));
            if (ret.data.status == 1) {
                //储存id

                toast.success({
                    title:"添加成功",
                    duration:2000
                });

            }else{
              toast.success({
                  title:"添加失败",
                  duration:2000
              });
            }
        });
    }
</script>
</html>
